<?php
function getTak(string $firstname, string $lastname) {
	$dao = MemberDAO::getInstance();
	$result = $dao->getTak($firstname, $lastname);
	if (is_null($result)) {
		return "geen lid";
	} else {
		return $result;
	}

}

function countKeuken(array &$store) {
	$amount = 0;
	if (isset($store['keuken'])) {
		$amount += count($store['keuken']);
		foreach ($store['leiding'] as $lentry) {
			foreach ($store['keuken'] as $key => $kentry) {
				if ($kentry->getLastname() == $lentry->getLastname()
				&& $kentry->getFirstname()==$lentry->getFirstname()) {
					$amount --;
					unset($store['keuken'][$key]);
				}
			}
		}
	}
	return $amount;
}

function countVeggies($entries) {
	$veggies = 0;
	if (is_array($entries)) {
		foreach ($entries as $entry) {
			foreach ($entry->getSupplements() as $supp) {
				if ($supp->getName()=="Vege") {
					$veggies++;
				}
			}
		}
	}
	return $veggies;
}

function isVeggie(Entry $entry) {
	$isVeggie = false;
	foreach ($entry->getSupplements() as $supp) {
		if ($supp->getName()=="Vege") {
			$isVeggie = true;
		}
	}
	return $isVeggie;
}

$events = array();
if ($eventid == "all") {
	$events = Event::getAllEvents();
} else {
	$events = array(Event::loadEvent((integer)$eventid));
}
?>
<div id="listinleiding">
<h3>Aanwezigheidslijsten</h3>
<ul>
<?php
if ($eventid == "all" || strstr(Event::loadEvent((integer)$eventid)->getPrefix(),"ZK")) {?>
	<li><a href="#takken">Alle takken</a></li>
	<li><a href="#leiding">De leiding</a></li>
	<li><a href="#keuken">De keukenploeg</a></li>
	<?php
}
if ($eventid == "all" || strstr(Event::loadEvent((integer)$eventid)->getPrefix(),"BD")) {?>
	<li><a href="#bezoekdag">Bezoekdag</a></li>
	<?php
}
?>
</ul>
</div>
<?php
foreach ($events as $event) {
	$entries = array();

	// Get all payed participants
	$total = 0;
	foreach ($event->getAllRegistrations() as $reg) {
		//if ($reg->isPayed()) {
		foreach ($reg->getEntries() as $entry) {
			$entries[] = $entry;
			$total++;
		}
		//}
	}



	// store in entrystore
	$entrystore = array(
					'bevers' => array(),
					'welpen' => array(),
					'wolven' => array(),
					'jvgs' => array(),
					'vgs' => array(),
					'seniors' => array(),
					'leiding' => array(),
					'keuken' => array(),
					'comite' => array(),
					'stam' => array(),
					'geen lid' => array());
	foreach ($entries as $entry) {
		// count
		$tak = getTak($entry->getFirstname(), $entry->getLastname());
		if (isset($entrystore[$tak])) {
			$entrystore[$tak][] = $entry;
		} else {
			$entrystore[$tak] = array($entry);
		}
		$groupname = $entry->getPriceCategory()->getName();
		if (strpos($groupname, "kookploeg") || strpos($groupname, "keuken")) {
			if (isset($entrystore['keuken'])) {
				$entrystore['keuken'][] = $entry;
			} else {
				$entrystore['keuken'] = array($entry);
			}
		}
		else if ($groupname== "kind") {
			if (isset($entrystore['kind'])) {
				$entrystore['kind'][] = $entry;
			} else {
				$entrystore['kind'] = array($entry);
			}
		}
		else if ($groupname == "volwassene") {
			if (isset($entrystore['volwassene'])) {
				$entrystore['volwassene'][] = $entry;
			} else {
				$entrystore['volwassene'] = array($entry);
			}
		}
	}
?>

<?php if (strstr($event->getPrefix(),"ZK")) {
	countKeuken($entrystore);
}
?>
<?php if (strstr($event->getPrefix(),"ZK")) {?>
<div class="eventcontainer">

	<h4>
		<a class="anchor" name="takken"><?php echo $event->getName()." : Alle takken";?>
		</a> (<a href="javascript:copy('tableTakken')">copy to excel</a>)
	</h4>
	<table width="98%" id="tableTakken">
		<thead>
			<tr>
				<th>#</th>
				<th>Naam</th>
				<th>Voornaam</th>
				<th>Tak</th>
				<th>Veggie</th>
				<th>Betaald</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$index = 0;
		foreach ($entrystore as $tak => $entries) {
			if ($tak=="bevers" || $tak=="welpen" || $tak=="wolven" || $tak=="jvgs" || $tak=="vgs") {
				foreach ($entries as $entry) {
					?>
			<tr>
				<td><?php echo $index;?></td>
				<td><?php echo ucfirst($entry->getLastname());?></td>
				<td><?php echo ucfirst($entry->getFirstname());?></td>
				<td><?php echo $tak;?></td>
				<td><?php echo isVeggie($entry) ? "ja": "nee";?></td>
				<td><?php echo $entry->getRegistration()->isPayed()?"ja":"nee";?></td>
			</tr>
			<?php
			$index++;
				}
			}
		}
		?>

		</tbody>
	</table>
</div>
		<?php }?>
		<?php if (strstr($event->getPrefix(),"ZK")) {?>
<div class="eventcontainer">

	<h4>
		<a class="anchor" name="leiding"><?php echo $event->getName()." : Leiding";?>
		</a> (<a href="javascript:copy('tableLeiding')">copy to excel</a>)
	</h4>
	<table width="98%" id="tableLeiding">
		<thead>
			<tr>
				<th>#</th>
				<th>Naam</th>
				<th>Voornaam</th>
				<th>Tak</th>
				<th>Veggie</th>
				<th>Betaald</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$index = 0;
		foreach ($entrystore['leiding'] as $entry) {
			?>
			<tr>
				<td><?php echo $index;?></td>
				<td><?php echo ucfirst($entry->getLastname());?></td>
				<td><?php echo ucfirst($entry->getFirstname());?></td>
				<td>Leiding</td>
				<td><?php echo isVeggie($entry) ? "ja": "nee";?></td>
				<td><?php echo $entry->getRegistration()->isPayed()?"ja":"nee";?></td>
			</tr>
			<?php
			$index++;
		}
		?>

		</tbody>
	</table>
</div>
		<?php }?>
		<?php if (strstr($event->getPrefix(),"ZK")) {?>
<div class="eventcontainer">

	<h4>
		<a class="anchor" name="keuken"><?php echo $event->getName()." : Keukenploeg";?>
		</a> (<a href="javascript:copy('tableKeuken')">copy to excel</a>)
	</h4>
	<table width="98%" id="tableKeuken">
		<thead>
			<tr>
				<th>#</th>
				<th>Naam</th>
				<th>Voornaam</th>
				<th>Tak</th>
				<th>Veggie</th>
				<th>Betaald</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$index = 0;
		foreach ($entrystore['keuken'] as $entry) {
			?>
			<tr>
				<td><?php echo $index;?></td>
				<td><?php echo ucfirst($entry->getLastname());?></td>
				<td><?php echo ucfirst($entry->getFirstname());?></td>
				<td><?php echo getTak($entry->getFirstname(), $entry->getLastname());?>
				</td>
				<td><?php echo isVeggie($entry) ? "ja": "nee";?></td>
				<td><?php echo $entry->getRegistration()->isPayed()?"ja":"nee";?></td>
			</tr>
			<?php
			$index++;
		}
		?>

		</tbody>
	</table>
</div>
		<?php }?>
		<?php if (strstr($event->getPrefix(),"BD")) {?>
<div class="eventcontainer">

	<h4>
		<a class="anchor" name="bezoekdag"><?php echo $event->getName()." : Deelnemers";?>
		</a> (<a href="javascript:copy('tableBezoekdag')">copy to excel</a>)
	</h4>
	<table width="98%" id="tableBezoekdag">
		<thead>
			<tr>
				<th>#</th>
				<th>Naam</th>
				<th>Voornaam</th>
				<th>Leeftijd</th>
				<th>Tak</th>
				<th>Betaald</th>
				<th>Verantwoordelijk</th>
				<th>Email</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$index = 0;
		foreach ($entrystore as $tak => $entries) {
			if ($tak=="kind" || $tak=="volwassene") {
				foreach ($entries as $entry) {?>
					<tr>
						<td><?php echo $index;?></td>
						<td><?php echo ucfirst($entry->getLastname());?></td>
						<td><?php echo ucfirst($entry->getFirstname());?></td>
						<td><?php echo $tak;?></td>
						<td><?php echo getTak($entry->getFirstname(), $entry->getLastname());?>
						</td>
						<td><?php echo $entry->getRegistration()->isPayed()?"ja":"nee";?></td>
						<td><?php echo ucwords($entry->getRegistration()->getResponsibleName());?></td>
						<td><?php echo $entry->getRegistration()->getEmail();?></td>
					</tr>
					<?php
					$index++;
				}
			}
		}
		?>
		</tbody>
	</table>
</div>
	<?php
	}
}
?>